from common.flask_package import *
from model.place import *
from model.log import *
from common.my_msg import *
from common.my_fun import *
import json

bp_place = Blueprint('place', __name__, url_prefix='/place')
# ----------增加车位--------------------
@bp_place.route('/add',methods=['POST'])
def place_add():
    if request.method=='POST':
        if request.method=='POST':
            num=request.json.get('num')
            relust=Place.add_place_number(num)
            if relust==0:
                Log.add_log('管理员', f'增加{num}个车位')  # 添加操作记录
                data={
                    'code':state_ok(),
                    'msg':f'成功增加{num}个车位'
                }
            else:
                data = {
                    'code': state_error(),
                    'msg': '车位增加失败'
                }

        return jsonify(data)

#------------------ 修改车位------------------------------
@bp_place.route('/modify/state',methods=['POST'])
def place_modify_state():
    if request.method=='POST':
        place_id=request.json.get('place_id')
        relust=Place.modify_place_state(place_id)
        query=Place.query_place_id_first(place_id)
        if relust==0:
            Log.add_log('管理员', f'关闭{query.place_number}号车位')
            data={
                'code':state_ok(),
                'msg': '车位关闭成功',
                'static': 'False'
            }
        elif relust==1:
            Log.add_log('管理员', f'开放{query.place_number}号车位')
            data = {
                'code': state_ok(),
                'msg': '车位开放成功',
                'static':'True'
            }
        elif relust==-2:
            data = {
                'code': state_warn(),
                'msg': '该车位有车辆停留,无法修改',
                'static': ''
            }
        else:
            data = {
                'code': state_error(),
                'msg': '车位修改失败',
                'static': ''
            }
    return jsonify(data)

#-------------------------获取车位信息列表------------------------------
@bp_place.route('/get',methods=['GET'])
def place_get():
    if request.method=='GET':
        page = request.args.get('page', default=1)
        car_number = request.args.get('car_number')
        place_number = request.args.get('place_number')
        count=1
        if car_number is not None and car_number != '':
            result = Place.place_car_number_search(car_number)
            count = 1
        elif place_number is not None and place_number != '':
            result = Place.place_number_search(place_number)
        else:
            result, count = Place.query_place_page(page)
        try:
            place_list = []
            for i in result:
                item = {
                    'place_id': i.place_id,
                    'place_number':i.place_number,
                    'car_number':i.car_number,
                    'car_state':i.car_state,
                    'place_state':i.place_state
                }
                place_list.append(item)

            data = {
                'code': state_ok(),
                'msg': '车位信息获取成功',
                'count': count,
                'data': place_list
            }
        except Exception as e:
            data = {
                'code': state_error(),
                'msg': '车位信息获取失败',
                'count': 1,
                'data': place_list
            }
            print(e)
        return Response(json.dumps(data), mimetype='application/json')

#------------------------删除车位-------------------------------
@bp_place.route('/del',methods=['DELETE'])
def place_del():
    if request.method=='DELETE':
        place_id=request.args.get('place_id')
        query = Place.query_place_id_first(place_id)
        relust=Place.del_place(place_id)
        if relust==0:
            Log.add_log('管理员', f'删除{query.place_number}号车位')
            data = {
                'code': state_ok(),
                'msg': '删除成功'
            }
        elif relust==-2:
            data = {
                'code': state_warn(),
                'msg': '该车位有车停留,无法删除该车位'
            }
        else:
            data = {
                'code': state_error(),
                'msg': '删除失败'
            }
    return jsonify(data)



'''
客户端
'''
@bp_place.route('/modify/car',methods=['POST'])
def place_modify_car():
    if request.method=='POST':
        car_number=request.json.get('car_number')
        relust=Place.modify_car_state(car_number)
        if relust==1:
            data = {
                'code': state_ok(),
                'msg': '车辆已修改为正常状态'
            }
        elif relust==0:
            data = {
                'code': state_ok(),
                'msg': '车辆已修改为故障状态'
            }
        else:
            data = {
                'code': state_error(),
                'msg': '车辆状态修改失败'
            }
    return jsonify(data)


'''
功能: 根据客户端输入的车牌号查询车辆是否在库
用途：前台客户端
'''
@bp_place.route('/is/car',methods=['POST'])
def place_is_car():
    if request.method=='POST':
        car_number=request.json.get('car_number')
        relust=Place.query_place_info(car_number)
        if relust:
            data={
                'code':state_ok(),
                'msg':'车辆在库'
            }
        else:
            data = {
                'code': state_error(),
                'msg': '该车辆不在库'
            }
    return jsonify(data)